"""
智能旅游助手Agent依赖安装脚本
安装LangGraph Agent所需的依赖包
"""

import subprocess
import sys
import os

def install_package(package):
    """安装Python包"""
    try:
        subprocess.check_call([sys.executable, "-m", "pip", "install", package])
        print(f"✅ 成功安装: {package}")
        return True
    except subprocess.CalledProcessError as e:
        print(f"❌ 安装失败: {package}, 错误: {e}")
        return False

def main():
    """主函数"""
    print("=" * 60)
    print("智能旅游助手 Agent 依赖安装")
    print("=" * 60)
    
    # 必需的依赖包
    required_packages = [
        "langgraph>=0.0.40",
        "langchain>=0.1.0",
        "langchain-community>=0.0.20",
        "matplotlib>=3.5.0",
        "numpy>=1.21.0",
        "requests>=2.25.0",
        "python-dotenv>=0.19.0",
        "qdrant-client>=1.6.0",
        "dashscope>=1.14.0"
    ]
    
    # 可选依赖包
    optional_packages = [
        "jupyter>=1.0.0",
        "pytest>=6.0.0",
        "pytest-mock>=3.6.0"
    ]
    
    print("正在安装必需的依赖包...")
    print("-" * 40)
    
    failed_packages = []
    
    for package in required_packages:
        if not install_package(package):
            failed_packages.append(package)
    
    print("\n正在安装可选的依赖包...")
    print("-" * 40)
    
    for package in optional_packages:
        install_package(package)
    
    print("\n" + "=" * 60)
    print("安装结果总结")
    print("=" * 60)
    
    if failed_packages:
        print(f"❌ 以下必需包安装失败:")
        for package in failed_packages:
            print(f"   - {package}")
        print("\n请手动安装失败的包，然后重新运行Agent。")
        return False
    else:
        print("✅ 所有必需依赖包安装成功！")
        print("\nAgent现在可以正常使用了。")
        return True

if __name__ == "__main__":
    success = main()
    if not success:
        sys.exit(1)
